import Vue from 'vue'
import Router from 'vue-router'
import Container from '../views/container.vue'
import Manager from '../views/manager.vue'
import Login from '../views/Login.vue'
import { createRoute, getRoutePermission } from '@/router/router.config';
import store from '@/store';
Vue.use(Router)

const staticRoute = [
  {
    path: '/',
    name: 'Container',
    component: Container,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: {
          title: '首页'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/Home')
      },
      {
        path: '/depIntroduce',
        name: 'DepIntroduce',
        meta: {
          title: '本部介绍',
          subtitle: '现任领导'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/DepIntroduce')
      },
      {
        path: '/mainFunction',
        name: 'MainFunction',
        meta: {
          title: '本部介绍',
          subtitle: '主要职能'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/MainFunction.vue')
      },
      {
        path: '/newsCenter',
        name: 'NewsCenter',
        meta: {
          title: '新闻中心',
          subtitle: '本地要闻'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NewsCenter')
      },
      {
        path: '/workTrend',
        name: 'workTrend',
        meta: {
          title: '新闻中心',
          subtitle: '工作动态'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/WorkTrend')
      },
      {
        path: '/newsPolitical',
        name: 'NewsPolitical',
        meta: {
          title: '新闻中心',
          subtitle: '时政要闻'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NewsPolitical')
      },
      {
        path: '/inforBulletin',
        name: 'InforBulletin',
        meta: {
          title: '门户信息',
          subtitle: '信息公开'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/InforBulletin')
      },
      {
        path: '/publicAnnouncement',
        name: 'PublicAnnouncement',
        meta: {
          title: '门户信息',
          subtitle: '公示公告'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/PublicAnnouncement.vue')
      },
      {
        path: '/depBudgets',
        name: 'DepBudgets',
        meta: {
          title: '门户信息',
          subtitle: '部门预决算'
        },
        component: () => import('@/views/pages/DepBudgets.vue')
      },
      {
        path: '/basicUnitedFront',
        name: 'BasicUnitedFront',
        meta: {
          title: '基层统战',
          subtitle: '基层统战'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/BasicUnitedFront')
      },
      {
        path: '/searchList',
        name: 'SearchList',
        meta: {
          title: '搜索内容',
          subtitle: '搜索内容'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/SearchListPage')
      },
      {
        path: '/multiPartyCooperation',
        name: 'MultiPartyCooperation',
        meta: {
          title: '多党合作',
          subtitle: '多党合作'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/MultiPartyCooperation')
      },
      {
        path: '/nationalReligion',
        name: 'NationalReligion',
        meta: {
          title: '民族宗教',
          subtitle: '民族宗教'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NationalReligion')
      },
      {
        path: '/emigrant',
        name: 'Emigrant',
        meta: {
          title: '港澳台侨',
          subtitle: '港澳台侨'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/Emigrant')
      },
      {
        path: '/article',
        name: 'Article',
        meta: {
          title: '内容详情',
          subtitle: '内容详情'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/Article')
      },
      {
        path: '/nonePublicEconomy',
        name: 'NonePublicEconomy',
        meta: {
          title: '非公经济',
          subtitle: '非公经济'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NonePublicEconomy')
      },
      {
        path: '/newSocialClasses',
        name: 'NewSocialClasses',
        meta: {
          title: '党外知识分子和新的社会阶层',
          subtitle: '党外知识分子和新的社会阶层'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NewSocialClasses')
      },
      {
        path: '/learnNewTheory',
        name: 'LearnNewTheory',
        meta: {
          // title: '机关党建',
          title: '学习新语',
          subtitle: '学习新语'
        },
        component: () => import('@/views/pages/LearnNewTheory.vue')
      },
      {
        path: '/theoreticalGarden',
        name: 'TheoreticalGarden',
        meta: {
          // title: '机关党建',
          title: '党的建设',
          subtitle: '理论园地'
        },
        component: () => import('@/views/pages/TheoreticalGarden.vue')
      },
      {
        path: '/partyBuilding',
        name: 'PartyBuilding',
        meta: {
          title: '党的建设',
          subtitle: '党的建设'
        },
        component: () => import('@/views/pages/PartyBuilding.vue')
      },
      {
        path: '/learnConferenceSpirit',
        name: 'LearnConferenceSpirit',
        meta: {
          title: '党的建设',
          subtitle: '学习贯彻党的二十届三中全会精神'
        },
        component: () => import('@/views/pages/LearnConferenceSpirit.vue')
      }
      // {
      //   path: '/mechanismSetup',
      //   name: 'MechanismSetup',
      //   meta: {
      //     title: '本部介绍',
      //     subtitle: '机构设置'
      //   },
      //   component: () => import('@/views/pages/MechanismSetup.vue')
      // },
      // {
      //   path: '/windowOfLeadership',
      //   name: 'WindowOfLeadership',
      //   meta: {
      //     title: '本部介绍',
      //     subtitle: '领导之窗'
      //   },
      //   component: () => import('@/views/pages/WindowOfLeadership.vue')
      // }
    ]
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/404',
    component: () => import('@/views/404')
  }
]
export const constantRoutes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/manager',
    redirect: '/welcome'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    name: 'Container',
    component: Container,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: {
          title: '首页'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/Home')
      },
      {
        path: '/searchList',
        name: 'SearchList',
        meta: {
          title: '搜索内容',
          subtitle: '搜索内容'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/SearchListPage')
      },
      {
        path: '/depIntroduce',
        name: 'DepIntroduce',
        meta: {
          title: '本部介绍',
          subtitle: '现任领导'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/DepIntroduce')
      },
      {
        path: '/mainFunction',
        name: 'MainFunction',
        meta: {
          title: '本部介绍',
          subtitle: '主要职能'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/MainFunction.vue')
      },
      {
        path: '/newsCenter',
        name: 'NewsCenter',
        meta: {
          title: '新闻中心',
          subtitle: '本地要闻'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NewsCenter')
      },
      {
        path: '/workTrend',
        name: 'workTrend',
        meta: {
          title: '新闻中心',
          subtitle: '工作动态'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/WorkTrend')
      },
      {
        path: '/newsPolitical',
        name: 'NewsPolitical',
        meta: {
          title: '新闻中心',
          subtitle: '时政要闻'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NewsPolitical')
      },
      {
        path: '/inforBulletin',
        name: 'InforBulletin',
        meta: {
          title: '门户信息',
          subtitle: '信息公开'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/InforBulletin')
      },
      {
        path: '/publicAnnouncement',
        name: 'PublicAnnouncement',
        meta: {
          title: '门户信息',
          subtitle: '公示公告'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/PublicAnnouncement.vue')
      },
      {
        path: '/depBudgets',
        name: 'DepBudgets',
        meta: {
          title: '门户信息',
          subtitle: '部门预决算'
        },
        component: () => import('@/views/pages/DepBudgets.vue')
      },
      {
        path: '/basicUnitedFront',
        name: 'BasicUnitedFront',
        meta: {
          title: '基层统战',
          subtitle: '基层统战'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/BasicUnitedFront')
      },
      {
        path: '/article',
        name: 'Article',
        meta: {
          title: '内容详情',
          subtitle: '内容详情'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/Article')
      },
      {
        path: '/multiPartyCooperation',
        name: 'MultiPartyCooperation',
        meta: {
          title: '多党合作',
          subtitle: '多党合作'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/MultiPartyCooperation')
      },
      {
        path: '/nationalReligion',
        name: 'NationalReligion',
        meta: {
          title: '民族宗教',
          subtitle: '民族宗教'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NationalReligion')
      },
      {
        path: '/emigrant',
        name: 'Emigrant',
        meta: {
          title: '港澳台侨',
          subtitle: '港澳台侨'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/Emigrant')
      },
      {
        path: '/nonePublicEconomy',
        name: 'NonePublicEconomy',
        meta: {
          title: '非公经济',
          subtitle: '非公经济'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NonePublicEconomy')
      },
      {
        path: '/newSocialClasses',
        name: 'NewSocialClasses',
        meta: {
          title: '党外知识分子和新的社会阶层',
          subtitle: '党外知识分子和新的社会阶层'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/pages/NewSocialClasses')
      },
      {
        path: '/learnNewTheory',
        name: 'LearnNewTheory',
        meta: {
          // title: '机关党建',
          title: '学习新语',
          subtitle: '学习新语'
        },
        component: () => import('@/views/pages/LearnNewTheory.vue')
      },
      {
        path: '/theoreticalGarden',
        name: 'TheoreticalGarden',
        meta: {
          // title: '机关党建',
          title: '党的建设',
          subtitle: '理论园地'
        },
        component: () => import('@/views/pages/TheoreticalGarden.vue')
      },
      {
        path: '/partyBuilding',
        name: 'PartyBuilding',
        meta: {
          title: '党的建设',
          subtitle: '党的建设'
        },
        component: () => import('@/views/pages/PartyBuilding.vue')
      },
      {
        path: '/learnConferenceSpirit',
        name: 'LearnConferenceSpirit',
        meta: {
          title: '党的建设',
          subtitle: '学习贯彻党的二十届三中全会精神'
        },
        component: () => import('@/views/pages/LearnConferenceSpirit.vue')
      }
      // {
      //   path: '/mechanismSetup',
      //   name: 'MechanismSetup',
      //   meta: {
      //     title: '本部介绍',
      //     subtitle: '机构设置'
      //   },
      //   component: () => import('@/views/pages/MechanismSetup.vue')
      // },
      // {
      //   path: '/windowOfLeadership',
      //   name: 'WindowOfLeadership',
      //   meta: {
      //     title: '本部介绍',
      //     subtitle: '领导之窗'
      //   },
      //   component: () => import('@/views/pages/WindowOfLeadership.vue')
      // }
    ]
  },
  {
    path: '/manager',
    name: 'Manager',
    component: Manager,
    children: [
      {
        path: '/welcome',
        component: () => import('@/views/welcome')
      },
      {
        path: '/contentRelease',
        name: 'ContentRelease',
        meta: {
          title: '内容发布'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/info/ContentRelease')
      },
      {
        path: '/contentAudit',
        name: 'ContentAudit',
        meta: {
          title: '内容审核'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/info/ContentAudit')
      },
      {
        path: '/systemManager',
        name: 'SystemManager',
        meta: {
          title: '人员管理'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/system/systemManager')
      },
      {
        path: '/department',
        name: 'Department',
        meta: {
          title: '组织架构管理'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/system/department')
      },
      {
        path: '/systemMenu',
        name: 'SystemMenu',
        meta: {
          title: '菜单管理'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/system/systemMenu')
      },
      {
        path: '/systemRole',
        name: 'SystemRole',
        meta: {
          title: '角色管理'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/system/systemRole')
      },
      {
        path: '/bannerAudit',
        name: 'BannerAudit',
        meta: {
          title: '专题审核'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/bannerManager/BannerAudit')
      },
      {
        path: '/bannerRelease',
        name: 'BannerRelease',
        meta: {
          title: '专题发布'
        },
        component: () => import(/* webpackChunkName: "table" */ '@/views/bannerManager/BannerRelease')
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/404')
  },
  // 404 页面必须放在末尾
  { path: '*', redirect: '/404' }
]
const createRouter = () => new Router({
  // mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置路由
}
router.beforeEach(async(to, from, next) => {
  if (getRoutePermission(staticRoute, to.path)) {
    next()
  } else {
    if (store && store.state.user.user_token.accessToken !== '') {
      if (store && store.state.user.menu_info.length === 0) {
        let route = await createRoute();
        route = staticRoute.concat(route);
        await store.dispatch('user/SET_MENU_INFO', route);
        const permission = getRoutePermission(route, to.path);
        if (permission) {
          // next({ ...to });
          next();
        } else if (to.path === '/welcome') {
          next();
        } else {
          next('/404');
        }
      } else {
        const data = store.state.user.menu_info;
        const permission = getRoutePermission(data, to.path);
        if (permission) {
          next();
        } else {
          next();
          // next('/404'); //会异常拦截
        }
      }
    } else {
      next('/login');
    }
  }
})
export default router

